package com.supor.aiot.common.adapter;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.Lifecycle;
import androidx.viewbinding.ViewBinding;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.supor.aiot.common.base.BaseMVVMFragment;

import java.util.List;

/**
 * Created by jtom4722 on 2020/12/25
 */
public class VP2FragmentStateAdapter<T extends BaseMVVMFragment<? extends AndroidViewModel, ? extends ViewDataBinding>> extends FragmentStateAdapter {
    private List<T> fragments;

    public VP2FragmentStateAdapter(@NonNull FragmentActivity fragmentActivity, List<T> fragments) {
        super(fragmentActivity);
        init(fragments);
    }

    public VP2FragmentStateAdapter(@NonNull Fragment fragment, List<T> fragments) {
        super(fragment);
        init(fragments);
    }

    public VP2FragmentStateAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<T> fragments) {
        super(fragmentManager, lifecycle);
        init(fragments);
    }

    private void init(List<T> fragments) {
        this.fragments = fragments;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
        return fragments.size();
    }

    public List<T> getDatas() {
        return fragments;
    }
}
